package com.example.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.DeptEntity;
import com.example.domain.XsWQEntity;
import com.example.mapper.DeptMapper;
import com.example.service.XsWQService;
import com.example.utils.DateUtils;
import com.example.utils.config.CommonConfig;
import com.example.utils.poi.ExcelUtil;

/**
 * 销售 阶段性考核评分
 * 回款额 获取 进账申请书流程 里的进账金额  对应销售合同信息里  验收金额， 工程编号-->销售合同管理流程的 工程编号，销售经理负责人
 * @author user
 *
 */
@RestController
@RequestMapping("/api/xswq")
public class XsWQController {

	@Autowired
    private XsWQService xsWQService;
	
	/**
	 * 返回list  每人多条记录 
	 * http://localhost:9000/api/xswq/list?beginDate=2021-09-01&endDate=2021-09-30
	 */
	@RequestMapping("/list")
    public Object queryData(String beginDate,String endDate){
        return xsWQService.queryXsWQData(beginDate,endDate);
    }

	/**
	 * 返回map   每人一条记录，外勤得地方，未统计数字
	 * http://localhost:9000/api/xswq/map1?beginDate=2021-09-01&endDate=2021-09-30
	 */
	@RequestMapping("/map1")
    public Object map1(String beginDate,String endDate){
		Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
		List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
		for (XsWQEntity xsWQEntity : list) {
			if(map.get(xsWQEntity.getLoginid()) == null) {
				Map<String, Set<String>>  pmap = xsWQEntity.getProvinceMap();
				if(pmap == null) {
					pmap = new HashMap<String, Set<String>>();
				}
				Set<String> citylist = pmap.get(xsWQEntity.getProvince());
				if (citylist == null) {
					citylist = new HashSet<String>();
				}
				citylist.add(xsWQEntity.getCity());
				pmap.put(xsWQEntity.getProvince(), citylist);
				xsWQEntity.setProvinceMap(pmap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}else {
				XsWQEntity old = map.get(xsWQEntity.getLoginid());
				Map<String, Set<String>>  pmap = old.getProvinceMap();
				if(pmap == null) {
					pmap = new HashMap<String, Set<String>>();
				}
				Set<String> citylist = pmap.get(xsWQEntity.getProvince());
				if (citylist == null) {
					citylist = new HashSet<String>();
				}
				citylist.add(xsWQEntity.getCity());
				pmap.put(xsWQEntity.getProvince(), citylist);
				xsWQEntity.setProvinceMap(pmap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}
		}
        return map;
    }
	
	/**
	 * 返回map   每人一条记录，外勤得地方得日期
	 * http://localhost:9000/api/xswq/map2?beginDate=2021-09-01&endDate=2021-09-30
	 */
	@RequestMapping("/map2")
    public Object map2(String beginDate,String endDate){
		Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
		List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
		for (XsWQEntity xsWQEntity : list) {
			if(map.get(xsWQEntity.getLoginid()) == null) {
				Map<String, Map<String,Set<String>>>  pmap = xsWQEntity.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}else {
				XsWQEntity old = map.get(xsWQEntity.getLoginid());
				Map<String, Map<String,Set<String>>>  pmap = old.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}
		}
        return map;
    }
	
	/**
	 * 返回map   每人一条记录，外勤得地方的次数
	 * http://localhost:9000/api/xswq/map3?beginDate=2021-09-01&endDate=2021-09-30
	 */
	@RequestMapping("/map3")
    public Object map3(String beginDate,String endDate){
		Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
		List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
		for (XsWQEntity xsWQEntity : list) {
			if(map.get(xsWQEntity.getLoginid()) == null) {
				Map<String, Map<String,Set<String>>>  pmap = xsWQEntity.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = xsWQEntity.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}else {
				XsWQEntity old = map.get(xsWQEntity.getLoginid());
				Map<String, Map<String,Set<String>>>  pmap = old.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = old.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}			
		}
        return map;
    }
	
	/**
	 * 返回list   每人一条记录，外勤得地方的次数
	 * http://localhost:9000/api/xswq/list1?beginDate=2021-09-01&endDate=2021-09-30
	 */
	@RequestMapping("/list1")
    public Object list1(String beginDate,String endDate){
		Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
		List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
		for (XsWQEntity xsWQEntity : list) {
			if(map.get(xsWQEntity.getLoginid()) == null) {
				Map<String, Map<String,Set<String>>>  pmap = xsWQEntity.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = xsWQEntity.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}else {
				XsWQEntity old = map.get(xsWQEntity.getLoginid());
				Map<String, Map<String,Set<String>>>  pmap = old.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = old.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}			
		}
        return map.values().stream().collect(Collectors.toList());
    }
	
	/**
	 * 返回list  
	 * 数据参考格式：
	 * provinceCount  出差省份个数
     * cityCount      出差地级市个数
     * ccDays         出差天数
     * ycqts          应出勤天数
     * ccl            出差率
     * pccount        出差省/地级市天数详情
	[
    {
        "loginid":"20008",
        "deptid":143,
        "deptname":"华中分公司",
        "lastname":"兰天",
        "operatedate":"2021-09-30",
        "operatetime":"22:45:16",
        "longitude":"119.953947",
        "latitude":"36.772162",
        "address":"青岛市平度市南宁路16号龙腾南关佳苑",
        "province":"山东省",
        "city":"青岛市",
        "msg":null,
        "provinceMap":null,
        "pccount":{
            "山东省":{
                "烟台市":2,
                "潍坊市":1,
                "济宁市":12,
                "青岛市":4,
                "济南市":5
            },
            "北京市":{
                "廊坊市":1
            },
            "河北省":{
                "邯郸市":2,
                "石家庄市":4
            }
        },
        "pmcm":{
            "山东省":{
                "烟台市":[
                    "2021-09-24",
                    "2021-09-23"
                ],
                "潍坊市":[
                    "2021-09-24"
                ],
                "济宁市":[
                    "2021-09-10",
                    "2021-09-02",
                    "2021-09-13",
                    "2021-09-03",
                    "2021-09-14",
                    "2021-09-01",
                    "2021-09-06",
                    "2021-09-28",
                    "2021-09-15",
                    "2021-09-26",
                    "2021-09-27",
                    "2021-09-09"
                ],
                "青岛市":[
                    "2021-09-30",
                    "2021-09-22",
                    "2021-09-17",
                    "2021-09-18"
                ],
                "济南市":[
                    "2021-09-22",
                    "2021-09-23",
                    "2021-09-17",
                    "2021-09-15",
                    "2021-09-16"
                ]
            },
            "北京市":{
                "廊坊市":[
                    "2021-09-30"
                ]
            },
            "河北省":{
                "邯郸市":[
                    "2021-09-28",
                    "2021-09-29"
                ],
                "石家庄市":[
                    "2021-09-06",
                    "2021-09-07",
                    "2021-09-08",
                    "2021-09-09"
                ]
            }
        },
        "provinceCount":3,
        "cityCount":8,
        "ccDays":31,
        "ycqts":22,
        "ccl":140.9
    }
]
	 * http://localhost:9000/api/xswq/finallist?year=2022&month=2&dept=1
	 */
	@RequestMapping("/finallist")
    public Object finallist(String year,String month,String dept){
		
		List<XsWQEntity> finalList = new ArrayList<XsWQEntity>();
		
		List<DeptEntity> deptList = getDeptlist();
		String deptName = "";
		if(dept == null || "".equals(dept)) {
			// 没有传部门，查询所有的
		}else {
			deptName = deptList.get(Integer.parseInt(dept)).getValue();
		}
		List<XsWQEntity> list = getFinallist(year,month,dept);
		if("".equals(deptName)) { // 查询全部
			for (DeptEntity deptEntity : deptList) {
				XsWQEntity deptXsEntity = new XsWQEntity();
				deptXsEntity.setDeptname(deptEntity.getValue());
				int count = 0;
				double ccl = 0;
				for (XsWQEntity personXsEntity : list) {
					if(deptEntity.getValue().equals(personXsEntity.getDeptname())) {
						count ++ ;
						ccl = ccl + personXsEntity.getCcl();
					}
				}
				if(count == 0) {
					deptXsEntity.setCcl(0);	
				}else {
					int i = (int) (ccl/count * 100) ;
					deptXsEntity.setCcl(i/100.0);
				}
				finalList.add(deptXsEntity);
				for (XsWQEntity personXsEntity : list) {
					if(deptEntity.getValue().equals(personXsEntity.getDeptname())) {
						finalList.add(personXsEntity);
					}
				}
			}
		}else {
			XsWQEntity deptXsEntity = new XsWQEntity();
			deptXsEntity.setDeptname(list.get(0).getDeptname());
			int count = 0;
			double ccl = 0;
			for (XsWQEntity personXsEntity : list) {
				count ++ ;
				ccl = ccl + personXsEntity.getCcl();
			}
			if(count == 0) {
				deptXsEntity.setCcl(0);	
			}else {
				int i = (int) (ccl/count * 100) ;
				deptXsEntity.setCcl(i/100.0);
			}
			finalList.add(deptXsEntity);
			for (XsWQEntity personXsEntity : list) {
				finalList.add(personXsEntity);
			}
		}
		
		
        return finalList;
    }
	
	/**
	 * 导出
	 * http://localhost:9000/api/xswq/finalexportlist?year=2022&month=2&dept=1
	 * @param year
	 * @param month
	 * @param dept
	 * @return
	 */
	@RequestMapping("/finalexportlist")
    public String finalexportlist(String year,String month,String dept){
		
		List<XsWQEntity> finalList = new ArrayList<XsWQEntity>();
		
		List<DeptEntity> deptList = getDeptlist();
		String deptName = "";
		if(dept == null || "".equals(dept)) {
			// 没有传部门，查询所有的
		}else {
			deptName = deptList.get(Integer.parseInt(dept)).getValue();
		}
		List<XsWQEntity> list = getFinallist(year,month,dept);
		if("".equals(deptName)) { // 查询全部
			for (DeptEntity deptEntity : deptList) {
				XsWQEntity deptXsEntity = new XsWQEntity();
				deptXsEntity.setDeptname(deptEntity.getValue());
				int count = 0;
				double ccl = 0;
				for (XsWQEntity personXsEntity : list) {
					if(deptEntity.getValue().equals(personXsEntity.getDeptname())) {
						count ++ ;
						ccl = ccl + personXsEntity.getCcl();
					}
				}
				if(count == 0) {
					deptXsEntity.setCcl(0);	
				}else {
					int i = (int) (ccl/count * 100) ;
					deptXsEntity.setCcl(i/100.0);
				}
				finalList.add(deptXsEntity);
				for (XsWQEntity personXsEntity : list) {
					if(deptEntity.getValue().equals(personXsEntity.getDeptname())) {
						finalList.add(personXsEntity);
					}
				}
			}
		}else {
			XsWQEntity deptXsEntity = new XsWQEntity();
			deptXsEntity.setDeptname(list.get(0).getDeptname());
			int count = 0;
			double ccl = 0;
			for (XsWQEntity personXsEntity : list) {
				count ++ ;
				ccl = ccl + personXsEntity.getCcl();
			}
			if(count == 0) {
				deptXsEntity.setCcl(0);	
			}else {
				int i = (int) (ccl/count * 100) ;
				deptXsEntity.setCcl(i/100.0);
			}
			finalList.add(deptXsEntity);
			for (XsWQEntity personXsEntity : list) {
				finalList.add(personXsEntity);
			}
		}
		
        ExcelUtil<XsWQEntity> util = new ExcelUtil<XsWQEntity>(XsWQEntity.class);
        String fileName = util.exportExcel(finalList, "销售人员出差统计表"+year+"-"+month, CommonConfig.getProfile()+File.separator);
		return "/excel/"+fileName;
    }
	
	@RequestMapping("/deptlist")
    public Object deptlist(){
        return getDeptlist();
    }
	
	private List<DeptEntity> getDeptlist(){
		List<DeptEntity> list = new ArrayList<DeptEntity>();
		String deptNamesStr = CommonConfig.getDeptNames();
		String[] deptNames = deptNamesStr.split(",");
		for (int i=0;i<deptNames.length;i++) {
			DeptEntity deptEntity = new DeptEntity(i+"", deptNames[i]);
			list.add(deptEntity);
		}
        return list;
    }
	
	@Autowired
	DeptMapper deptMapper;
	// 查询每个月的应出勤天数
	private int queryWorkDaysByMonth(String yearmonth) {
		return deptMapper.queryWorkDaysByMonth(yearmonth);
	}
	
	private List<XsWQEntity> getFinallist(String year,String month,String dept){
			List<DeptEntity> deptList = getDeptlist();
			String deptName = "";
			if(dept == null || "".equals(dept)) {
				// 没有传部门，查询所有的
			}else {
				deptName = deptList.get(Integer.parseInt(dept)).getValue();
			}
			String beginDate = DateUtils.getMonthFirstDay(year, month);
			String endDate = DateUtils.getMonthLastDay(year, month);
			Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
			
			int count = queryWorkDaysByMonth(beginDate.substring(0,7));
			
			List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
			for (XsWQEntity xsWQEntity : list) {
				if(map.get(xsWQEntity.getLoginid()) == null) {
					Map<String, Map<String,Set<String>>>  pmap = xsWQEntity.getPmcm();
					if(pmap == null) {
						pmap = new HashMap<String, Map<String,Set<String>>>();
					}
					
					Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
					if (cityMap == null) {
						cityMap = new HashMap<String,Set<String>>();
						Set<String> timeSet = new HashSet<String>();
						timeSet.add(xsWQEntity.getOperatedate());
						cityMap.put(xsWQEntity.getCity(),timeSet);
					}else {
						Set<String> timeSet = new HashSet<String>();
						if(cityMap.get(xsWQEntity.getCity()) != null) {
							timeSet = cityMap.get(xsWQEntity.getCity());
						}
						timeSet.add(xsWQEntity.getOperatedate());
						cityMap.put(xsWQEntity.getCity(), timeSet);
					}
					pmap.put(xsWQEntity.getProvince(), cityMap);
					xsWQEntity.setPmcm(pmap);
					Map<String,Map<String,Integer>> pccountMap = xsWQEntity.getPccount();
					if(pccountMap == null) {
						pccountMap = new HashMap<String, Map<String,Integer>>();
					}
					Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
					if(countMap == null) {
						countMap = new HashMap<String, Integer>();
					}
					for (String key : cityMap.keySet()) {
						countMap.put(key, cityMap.get(key).size());
					}
					int cityCounts = 0;
					int ccdays = 0;
					for (String province : pccountMap.keySet()) {
						Map<String,Integer> cm = pccountMap.get(province);
						cityCounts += cm.size();
						for (String city : cm.keySet()) {
							ccdays += cm.get(city);
						}
					}
					pccountMap.put(xsWQEntity.getProvince(), countMap);
					xsWQEntity.setPccount(pccountMap);
					xsWQEntity.setProvinceCount(pccountMap.size()); // 出差省份个数
					xsWQEntity.setCityCount(cityCounts); // 出差地级市个数
					xsWQEntity.setCcDays(ccdays); // 出差天数
					map.put(xsWQEntity.getLoginid(), xsWQEntity);
				}else {
					XsWQEntity old = map.get(xsWQEntity.getLoginid());
					Map<String, Map<String,Set<String>>>  pmap = old.getPmcm();
					if(pmap == null) {
						pmap = new HashMap<String, Map<String,Set<String>>>();
					}
					Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
					if (cityMap == null) {
						cityMap = new HashMap<String,Set<String>>();
						Set<String> timeSet = new HashSet<String>();
						timeSet.add(xsWQEntity.getOperatedate());
						cityMap.put(xsWQEntity.getCity(),timeSet);
					}else {
						Set<String> timeSet = new HashSet<String>();
						if(cityMap.get(xsWQEntity.getCity()) != null) {
							timeSet = cityMap.get(xsWQEntity.getCity());
						}
						timeSet.add(xsWQEntity.getOperatedate());
						cityMap.put(xsWQEntity.getCity(), timeSet);
					}
					pmap.put(xsWQEntity.getProvince(), cityMap);
					xsWQEntity.setPmcm(pmap);
					Map<String,Map<String,Integer>> pccountMap = old.getPccount();
					if(pccountMap == null) {
						pccountMap = new HashMap<String, Map<String,Integer>>();
					}
					Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
					if(countMap == null) {
						countMap = new HashMap<String, Integer>();
					}
					for (String key : cityMap.keySet()) {
						countMap.put(key, cityMap.get(key).size());
					}
					int cityCounts = 0;
					int ccdays = 0;
					for (String province : pccountMap.keySet()) {
						Map<String,Integer> cm = pccountMap.get(province);
						cityCounts += cm.size();
						for (String city : cm.keySet()) {
							ccdays += cm.get(city);
						}
					}
					pccountMap.put(xsWQEntity.getProvince(), countMap);
					xsWQEntity.setPccount(pccountMap);
					xsWQEntity.setProvinceCount(pccountMap.size()); // 出差省份个数
					xsWQEntity.setCityCount(cityCounts); // 出差地级市个数
					xsWQEntity.setCcDays(ccdays); // 出差天数
					map.put(xsWQEntity.getLoginid(), xsWQEntity);
				}
			}
			String deptNamef = deptName;
	        return map.values().stream().filter((e) -> checkValue(e,deptNamef))
	        		.map(xsEntity ->{
	        			List<String> pcddList = xsEntity.getPcdds();// 排除地点
			        	xsEntity.setYcqts(count);
			        	if(count == 0) {
			        		xsEntity.setCcl(0);
			        	}else {
			        		xsEntity.setCcl(((int)xsEntity.getCcDays()*100*100/count)/(double)100);
			        	}
			        	StringBuffer sb = new StringBuffer();
			        	Map<String,Map<String,Integer>> pc = xsEntity.getPccount();
			        	for (String province : pc.keySet()) {
							Map<String,Integer> cm = pc.get(province);
							sb.append(province).append("\n\t");
							for (String city : cm.keySet()) {
								sb.append("    ").append(city).append(":").append(cm.get(city)).append("\n\t");
								if(pcddList.toString().contains(city)) { // 如果排除地点包含该地点，减去该城市出差天数
									xsEntity.setCcDays(xsEntity.getCcDays() - cm.get(city));// 出差天数重新计算
									if(xsEntity.getCcDays()<0) {
						        		xsEntity.setCcDays(0);
						        	}
									// 重新计算下出差率
									if(count == 0) {
						        		xsEntity.setCcl(0);
						        	}else {
						        		xsEntity.setCcl(((int)xsEntity.getCcDays()*100*100/count)/(double)100);// 出差率重新计算
						        	}
								}
							}
						}
			        	
			        	xsEntity.setShowMsg(sb.toString());
			        	return xsEntity;
			        	}).collect(Collectors.toList());
	    }

	private boolean checkValue(XsWQEntity xsEntity,String deptNamef) {
		if("".equals(deptNamef) || deptNamef.equals(xsEntity.getDeptname())) {
    		return true;
    	}else {
    		return false;
    	}
	}
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("烟台");
		list.add("青岛市");
		System.out.println(list.toString().contains("烟台市"));
	}
}

